package com.lovo.controller;

import com.google.gson.Gson;
import com.lovo.bean.Staff;
import com.lovo.service.IStaffService;
import com.lovo.service.serviceImpl.StaffServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "StaffServlet",urlPatterns = "/staff.do")
public class StaffServlet extends HttpServlet {
    private IStaffService iss =new StaffServiceImpl();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        String method = request.getParameter("method");
        if(method==null||method.equals("")){
            showStaffList(request,response);
        }else if(method.equals("addStaff")){
            addOneStaff(request,response);
        }else if(method.equals("deleteStaff")){
            deleteOneStaff(request,response);
        }
    }
    protected void showStaffList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("storeId");
        int storeId=0;
        if(id!=null){
            storeId =Integer.parseInt(id);
        }
        List<Staff> staffList = iss.selectByStoreKey(storeId);
        String json = new Gson().toJson(staffList);
        response.getWriter().write(json);
    }
    protected void addOneStaff(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String staffName = request.getParameter("staffName");
        String staffLevel = request.getParameter("staffLevel");
        String staffTel = request.getParameter("staffTel");
        String id = request.getParameter("fkStoreId");
        System.out.println("执行添加店员的方法");
        int fkStoreId =0;
        if(id!=null||!id.equals("")){
            fkStoreId =Integer.parseInt(id);
        }
        Staff staff =new Staff(staffName,staffLevel,staffTel,fkStoreId);
        System.out.println("staff = " + staff);
        boolean b = iss.insertStaff(staff);
        if(b){
            response.getWriter().write("true");
        }else{
            response.getWriter().write("false");
        }
    }
    protected void deleteOneStaff(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int staffId =0;
        String staffId1 = request.getParameter("staffId");
        if(staffId1!=null){
            staffId=Integer.parseInt(staffId1);
        }
        boolean b = iss.deleteStaff(staffId);
        if(b){
            response.getWriter().write("true");
        }else{
            response.getWriter().write("false");
        }
    }

}
